// Created by WXX on 2021/10/22 16:56
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {

        unordered_map<string, vector<string>> hash;
        for (auto &str : strs) {
            string s = str;
            sort(s.begin(), s.end());
            hash[s].push_back(str);
        }

        vector<vector<string>> res;
        for (auto &item : hash) res.push_back(item.second);

        return res;
    }
};

void OutputBasicArray2D1(vector<vector<string>> nums) {

    cout << "[";
    for (int i = 0; i < nums.size(); i++) {
        cout << "[";
        for (int j = 0; j < nums[i].size(); j++) {
            cout << nums[i][j];
            if (j != nums[i].size() - 1) cout << ", ";
        }
        cout << "]";
        if (i != nums.size() - 1) cout << ", ";
    }
    cout << "]" << endl;
}

int main() {

    vector<string> strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
    OutputBasicArray2D1(Solution().groupAnagrams(strs));

    return 0;
}
